package logic

import (
	"context"
	"errors"
	"strings"
	"video-manager/model/mysql"

	"video-manager/usersrpc/internal/svc"
	"video-manager/usersrpc/usersrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentAddLogic {
	return &CommentAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentAddLogic) CommentAdd(in *usersrpc.CommentAddRequest) (*usersrpc.CommentAddResponse, error) {
	// todo: add your logic here and delete this line
	var findUser mysql.User
	if err := findUser.FindUserByUserId(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	if findUser.ID == 0 {
		return nil, errors.New("用户不存在")
	}
	if strings.Contains("垃圾", in.CommentContent) || strings.Contains("骗人", in.CommentContent) {
		return nil, errors.New("含违规词汇，请修改")
	}
	comment := &mysql.Comment{
		//Model:           gorm.Model{},
		Video_id:        int(in.VideoId),
		User_id:         int(in.UserId),
		Comment_content: in.CommentContent,
		Is_anonymous:    int8(in.IsAnonymous),
	}
	err := comment.CommentAdd(mysql.DB)
	if err != nil {
		return nil, errors.New("评论发布失败")
	}
	return &usersrpc.CommentAddResponse{
		CommentId: int64(comment.ID),
	}, nil
}
